import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import pandas as pd
import tushare as ts
from utils.confutils import ConfUtil
import time
from tqdm import tqdm
import datetime

# 提取token
token = ConfUtil().get_tokens('tushare')
ts.set_token(token)
pro = ts.pro_api(token)
# 读取上交所交易日历，获得交易日清单
calendar_file = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\Data\\RawData\\calendar.feather"
df_calendar = pd.read_feather(calendar_file)
tradingdays = df_calendar.query("exchange=='SSE' and is_open==1").cal_date.sort_values().unique()
print(tradingdays)

# 循环下载股票日线数据（复权方式为前复权）,20100101至今
df_full = []
print('开始下载申万指数日线数据...')
for tradingday in tqdm(tradingdays):
    is_fail = True
    while 1==1:
        try:
            df = pro.sw_daily(trade_date=tradingday)
            break                  
        except Exception as e:
            time.sleep(5)
            continue
    df_full.append(df)
df_res = pd.concat(df_full,axis=0)
print(f'sw日线数据信息:\n {df.info()}')
print(df_res.head(5))

# 存到..\Data\RawData\stock_daily_bar.feather文件中
tofile = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\Data\\RawData\\index_sw_dailybar.feather"
df_res.to_feather(tofile)


